package com.dnjn.mes.biz.service.helper;

import com.dnjn.mes.biz.domain.MesEquipment;
import com.dnjn.mes.biz.domain.MesWorkforceConfig;
import com.dnjn.mes.biz.service.IMesEquipmentService;
import com.dnjn.mes.biz.service.IMesWorkforceConfigService;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Component("workShiftHelper")
public class WorkShiftHelper {
    public static int SHIFT_START = 8;//8:00  轮班 一天开始时间
    @Autowired
    private IMesWorkforceConfigService workforceConfigService;
    @Autowired
    private IMesEquipmentService equipmentService;

    @Accessors(chain = true)
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    @Getter
    @Setter
    public static class WorkShiftConf {
        private MesWorkforceConfig current;
        private MesWorkforceConfig before;
        private MesWorkforceConfig after;
    }

    @Accessors(chain = true)
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    @Getter
    @Setter
    public static class WorkShiftDuration implements Serializable {
        private LocalDateTime start;
        private LocalDateTime end;
        private Long workforceId;
        private String workforceName;
    }

    public WorkShiftDuration calcShiftDuration(MesWorkforceConfig config) {
        LocalTime startTime = config.getStartTime().minusHours(SHIFT_START),
                endTime = config.getEndTime().minusHours(SHIFT_START);
        LocalDateTime now = LocalDateTime.now().minusHours(SHIFT_START);//?

        LocalDateTime start = LocalDateTime.of(now.toLocalDate(), startTime).plusHours(SHIFT_START);//?
        LocalDateTime end = LocalDateTime.of(now.toLocalDate(), endTime).plusHours(SHIFT_START);

        return WorkShiftDuration.builder()
                .start(start)
                .end(end)
                .workforceId(config.getWorkforceId())
                .workforceName(config.getWorkforceName())
                .build();

    }

    /**
     *
     */
    public WorkShiftConf current() {
        MesEquipment mesEquipment = equipmentService.fetchCurrent();
        if (null == mesEquipment) {
            return null;
        }
        List<MesWorkforceConfig> list = workforceConfigService.lambdaQuery().eq(MesWorkforceConfig::getDeleted, "0").eq(MesWorkforceConfig::getEquipmentId,mesEquipment.getEquipmentId()).list();

        list.forEach(el -> {//换算成8点开始新一天的时钟?!
            el.setStartTime(el.getStartTime().minusHours(SHIFT_START));
            el.setEndTime(el.getEndTime().minusHours(SHIFT_START));
        });
        //list.sort((a, b) -> a.getStartTime().isBefore(b.getStartTime()) ? 0 : 1);//?
        list.sort(Comparator.comparing(MesWorkforceConfig::getStartTime));//?
        //Map<Long, MesWorkforceConfig> map = list.stream().collect(Collectors.toMap(MesWorkforceConfig::getWorkforceId, Function.identity()));
        //LocalDateTime now1 = LocalDateTime.now().minusHours(SHIFT_START);
        Integer curIdx = null;

        LocalTime now = LocalTime.now().minusHours(SHIFT_START);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            MesWorkforceConfig el = list.get(i);
            if (el.getStartTime().isBefore(now) && el.getEndTime().isAfter(now)) {//默认全天都有排班
                curIdx = i;
                break;
            }
        }

        //
        MesWorkforceConfig current = null, before = null, after = null;
        if (Objects.isNull(curIdx)) {//?
            after = list.stream().filter(el -> el.getStartTime().isAfter(now) ).findFirst().orElse(null);
            before =list.stream().filter(el->el.getEndTime().isBefore(now)).findFirst().orElse(null);
        } else {
            current = list.get(curIdx);
            int a = (curIdx + 1) % size;
            int b = (size + curIdx - 1) % size;
            after = list.get(a);
            before = list.get(b);
        }
        //Assert.notNull(curIdx, "not.found");//todo

        list.forEach(el -> {//换算回来
            el.setStartTime(el.getStartTime().plusHours(SHIFT_START));
            el.setEndTime(el.getEndTime().plusHours(SHIFT_START));
        });

        return WorkShiftConf.builder()
                .current(current)
                .before(before)
                .after(after)
                .build();
    }
}
